// pages/person/index.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    is_store_admin: 0,
    headTop: "",
    headHeight: "",
    sumHeight: "",
    is_user_show: true,
    is_user: 0,
    userUrl: "",
    userName: "",
    balance: "0.00", //余额
    coupon: 0, //优惠券
    pricecoupon: 0,
    cardcoupon: 0,
    goods: "0.00", //积分
    showUser: false,
    mobi: '',
    version: '版本号：v1.0.0',
    serviceList: [{
        name: "客服",
        url: "/image/customer.png",
      },
      {
        name: "优惠卷",
        url: "/image/exchange.png",
        link: "/pages/person/maincoupon/index",
      },
      {
        name: "收货地址",
        url: "/image/address.png",
        link: "/pages/person/address/index",
      },
      {
        name: "设置",
        url: "/image/setting.png",
        link: "/pages/person/setting/index",
      },

    ],
  },
  onLoad() {
    const accountInfo = wx.getAccountInfoSync();
    const version = accountInfo.miniProgram.version;

    const that = this;
    that.setData({
      version: version ? '版本号：' + version : that.version,
    });
    if (wx.getStorageSync("token")) {
      const token = wx.getStorageSync("token")

      app.http.post(app.api.userInfo, {
        token
      }, (res) => {

        that.setData({
          userUrl: res.data.member.avatar,
          userName: res.data.member.member_id,
          balance: res.data.member.balance,
          coupon: res.data.member.coupon,
          pricecoupon: res.data.member.pricecoupon,
          cardcoupon: res.data.member.cardcoupon,
          goods: res.data.member.goods,
          showUser: true,
          is_store_admin: res.data.member.store
        });

      })
    }
    that.setData({
      headTop: app.globalData.headTop,
      headHeight: app.globalData.headHeight,
      sumHeight: app.globalData.sumHeight,

    });
  },
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      console.log("设置选中项 2");
      this.getTabBar().setData({
        selected: 4,
      });
    }
  },
  //   登录
  login() {
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    } else if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 2
      });
    }
  },
  // 登录遮罩层
  close(e) {
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    let mobi = e.detail.mobile.substr(0, 3) + "****" + e.detail.mobile.substr(7, 4)
    this.setData({
      userUrl: e.detail.avatar,
      userName: e.detail.nickname,
      balance: e.detail.balance,
      coupon: e.detail.coupon,
      goods: e.detail.goods,
      showUser: true,
      mobi,
    });
    wx.setStorageSync('userInfo', e.detail)
    wx.setStorageSync("mobile", e.detail.mobile)

  },
  // 开通会员
  vip() {
    const that = this
    if (that.is_store_admin == 0) {
      //加盟
      wx.navigateTo({
        url: "/pages/index/join/index",
      });
    } else {
      console.log(that.data.is_store_admin)
      //门店管理
      wx.navigateTo({
        url: "/pages/index/storeselect/index?store_id="+that.data.is_store_admin[0].store_id,
      });
    }

  },
  // 积分
  linkTointegra() {
    const that = this
    if (wx.getStorageSync('token') == "") {
      that.login()
      return
    }
    console.log(this.data.goods)
    wx.navigateTo({
      url: "/pages/goods/recordex/index?goods=" + that.data.goods
    });
  },
  // 余额
  linkTocoinex() {
    const that = this
    if (wx.getStorageSync('token') == "") {
      that.login()
      return
    }
    wx.navigateTo({
      url: "/pages/person/coinex/index?money=" + this.data.balance,
    });
  },
  // 我的服务跳转
  linkTo(e) {
    const that = this
    const url = e.currentTarget.dataset.link;
    if (url == "/pages/person/setting/index") {
      console.log(1)
      if (wx.getStorageSync('token') == "") {
        that.login()
        return
      }
      wx.navigateTo({
        url,
      });
      return
    }
    if (wx.getStorageSync('token') == "") {
      that.login()
      return
    }
    wx.navigateTo({
      url,
    });
  },
  // 优惠券
  linkTopres() {
    const that = this
    if (wx.getStorageSync('token') == "") {
      that.login()
      return
    }
    wx.navigateTo({
      url: "/pages/person/maincoupon/index",
    });
  },

  // 团购券
  linkToprescoupon() {
    const that = this
    if (wx.getStorageSync('token') == "") {
      that.login()
      return
    }

    wx.navigateTo({
      url: "/pages/person/mainpricecoupon/index",
    });
  },

  // 团购券
  linkTopcardcoupon() {
    const that = this
    if (wx.getStorageSync('token') == "") {
      that.login()
      return
    }

    wx.navigateTo({
      url: "/pages/person/cardcoupon/index",
    });
  },
  onShareAppMessage() {

  },
  onShareTimeline() {}
});